const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');

import NetWork from '../../../utils/modules/NetWork';

const app = getApp();

Component({

  externalClasses: ['j-class', 'j-active-class'],
  properties: {
    time: {
      type: Number,
      value: 60
    },
    tip: {
      type: String,
      value: '发送验证码'
    },
    options: {
      type: Object,
      value: {
        url: '',
        data: {}
      }
    },
    tel: {
      type: String,
      value: '',
      require: true
    }
  },
  data: {
    yzm_status: true,
    _baseUrl: app.globalData._baseUrl,
    _tip: '',
    _time: 0
  },
  attached: function () {
    this.setData({
      _tip: this.data.tip,
      _time: this.data.time
    })
  },
  methods: {
    yzm() {

      let _that = this;
      let tel = _that.data.options.data.phone;
      let bool = false;
      let net = new NetWork(app.globalData._baseUrl)
     
     


      if (!_that.data.yzm_status) return false;

      let reg_tpl = /^1[3456789]\d{9}$/;

     

        // /^ 1[345678]\d{ 9 } $ /

      if (!reg_tpl.test(tel) || !tel) {
        $Toast({
          content: '手机格式不正确',
          type: 'warning'
        });
        bool = true
      }

      if (!bool) {
        net._request(
          _that.data.options,
          '发送验证码',
          res => {
            _that.triggerEvent('success', res);
            _that.setData({
              _tip: _that.data._time + '秒再发送',
              yzm_status: false,
              yzm_tel: tel
            });

            let yzm = setInterval(function () {
              var cd = _that.data._time - 1;
              if (cd <= 0) {
                clearInterval(yzm)
                _that.setData({
                  _tip: _that.data.tip,
                  yzm_status: true,
                  _time: _that.data.time
                });
              } else {
                _that.setData({
                  _tip: cd + '秒再发送',
                  _time: cd
                });
              }
            }, 1000)
          }, res => {
            $Toast({
              content:res.data.msg,
              type: 'none'
            })
            _that.triggerEvent('fail', res);
          }
        )

        // app.http({
        // 	obj : _that.data.options
        // }).then().catch()
      }

    },
  }
})